import { defineStore } from 'pinia';

export const m1Store = defineStore('m1Store', {
	state: () => ({
		l1: { xData: [], ykli: [], jnyi: [] },
		l2: [],
		l3: { xData: [], wx: [], zfb: [] },
		centre: { money: [], list: [], regions: [] }
	}),
	actions: {
		// 获取 L1 数据
		getL1() {
			let timer = new Date().getTime();
			for (let i = 0; i < 7; i++) this.l1.xData.unshift(this.disTime(timer - i * 86400000));
			this.l1.ykli = [100, 130, 140, 160, 170, 150, 150];
			for (let item of this.l1.ykli) this.l1.jnyi.push(item - 40);
		},
		// 获取 L2 数据
		getL2() {
			this.l2 = [
				{ name: 'F5未来商店', money: '￥56000.00', red: '￥5600.00', number: 15 },
				{ name: '18DOG宠物医院', money: '￥54323.00', red: '￥5432.00', number: 3 },
				{ name: 'K5电子烟', money: '￥45632.00', red: '￥4563.00', number: 25 },
				{ name: '婵娟烘焙', money: '￥34512.00', red: '￥3451.00', number: 7 },
				{ name: '宠缘宠物', money: '￥56423.00', red: '￥5642.00', number: 9 },
			];
		},
		// 获取 L3 数据
		getL3() {
			let timer = new Date().getTime();
			for (let i = 0; i < 7; i++) this.l3.xData.unshift(this.disTime(timer - i * 86400000));
			this.l3.wx = [100, 130, 140, 160, 170, 150, 150];
			for (let item of this.l3.wx) this.l3.zfb.push(item - 40);
		},
		// 获取中间数据
		getCentre(ChinaMap) {
			this.disCity(ChinaMap);
			this.createEarn();
			setInterval(() => { this.createEarn() }, 1000);
			
			this.centre.list = [
				{ title: '公司总数', number: 30 },
				{ title: '品牌总数', number: 56 },
				{ title: '资产总数', number: 1500 },
				{ title: '用户人数', number: 3000 },
			];
			
			let color = {
				'#0DB7F9': ['江苏', '浙江', '安徽', '福建', '江西', '上海'],
				'#6254E6': ['香港', '澳门', '湖南', '广东', '广西', '海南', '云南', '贵州'],
				'#A86CF2': ['四川', '甘肃', '宁夏', '青海', '重庆'],
				'#425DFF': ['黑龙江', '吉林', '辽宁', '内蒙古', '山东'],
				'#128E38': ['河北', '山西', '陕西', '河南', '湖北', '北京', '天津'],
				'#FA8E4A': ['新疆'],
				'#F7BA34': ['西藏'],
				'#FFFFFF': ['台湾']
			}
			for (let item of ChinaMap.features) {
				let { name } = item.properties;
				let areaColor = '';
				for (let key in color) {
					if (color[key].indexOf(name) >= 0) {
						areaColor = key;
						break;
					}
				}
				this.centre.regions.push({ name, itemStyle: { areaColor }, label: { show: true, fontSize: 10, color: '#FFF' }});
			}
		},
		
		// 处理时间
		disTime(timer) {
			const date = new Date(timer);
			const month = (date.getMonth() + 1).toString().padStart(2, '0');
			const day = date.getDate().toString().padStart(2, '0');
			
			return `${month}/${day}`;
		},
		// 生成随机数
		randomInt(min, max) {
			min = Math.ceil(min);
			max = Math.floor(max);
			return Math.floor(Math.random() * (max - min + 1)) + min;
		},
		// 生成总交易额
		createEarn() {
			let time = new Date().getTime() + this.randomInt(10000, 99000);
			this.centre.money = String(~~(time / 1000)).split('');
		},
		// 处理省市区
		disCity(ChinaMap) {
			for (let item of ChinaMap.features) {
				switch (item.properties.name) {
					case '新疆维吾尔自治区': item.properties.name = '新疆'; break;
					case '香港特别行政区': item.properties.name = '香港'; break;
					case '澳门特别行政区': item.properties.name = '澳门'; break;
					case '广西壮族自治区': item.properties.name = '广西'; break;
					case '宁夏回族自治区': item.properties.name = '宁夏'; break;
					case '香港特别行政区': item.properties.name = '香港'; break;
					case '香港特别行政区': item.properties.name = '香港'; break;
					case '香港特别行政区': item.properties.name = '香港'; break;
					case '香港特别行政区': item.properties.name = '香港'; break;
					case '香港特别行政区': item.properties.name = '香港'; break;
					case '内蒙古自治区': item.properties.name = '内蒙古'; break;
					case '西藏自治区': item.properties.name = '西藏'; break;
					default: item.properties.name = item.properties.name.slice(0, -1); break;
				};
			}
		}
	}
})